package com.alpha.crm;



import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
//import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Filterable;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope; 

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
//import org.ksoap2.transport.HttpTransportSE;
//import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;

import com.alpha.crm.R;






public class Question extends ListActivity {
	
	
	TextView tv;
	
	
	public final static  String SOAP_ACTION = "http://www.alpha.net.gr/ExecuteEBSCommand";

 	public final static   String OPERATION_NAME = "ExecuteEBSCommand"; 

	public final static  String NAMESPACE = "http://www.alpha.net.gr";

	public final static   String URL = "http://192.168.1.246/Ebs2Alpha/Service.asmx";

	// All static variables
	
	// XML node keys
	static final String KEY_ITEM = "AllClients"; // parent node
	static final String KEY_ID = "GID";
	final String KEY_NAME = "Name";
	static final String KEY_COST = "NumericField1";
	static final String KEY_DESC = "Code";


	
	
	//ArrayAdapter<String> madapter;


	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_question);

		 SoapObject Request = new SoapObject (NAMESPACE,OPERATION_NAME);
	        Request.addProperty("strCommand", "get_clients");
	        Request.addProperty("strCommandParameters", "%");
	        
	        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	        soapEnvelope.dotNet = true;
	        soapEnvelope.setOutputSoapObject(Request);
	        
	        
	        try {
	            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
	           
	            //this is the actual part that will call the webservice
	            androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
	           
	            // Get the SoapResult from the envelope body.
	            SoapObject result = (SoapObject)soapEnvelope.bodyIn;

	           // if(result != null)
	           // {
	                  //Get the first property and change the label text
	           
	                  //tv.setText(result.getProperty(0).toString());
	                //  tv.setText("Customers : " + result);
	           // }
	          //  else
	          //  {
	           //       Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
	           // }
	            ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

	    		XMLParser parser = new XMLParser();
	    		String xml = result.getProperty(0).toString(); // getting XML
	    		Document doc = parser.getDomElement(xml); // getting DOM element

	    		NodeList nl = doc.getElementsByTagName(KEY_ITEM);{
	    		// looping through all item nodes <item>
	    		for (int i = 0; i < nl.getLength(); i++) {
	    			// creating new HashMap
	    			HashMap<String, String> map = new HashMap<String, String>();
	    			Element e = (Element) nl.item(i);
	    			// adding each child node to HashMap key => value
	    			map.put(KEY_ID, parser.getValue(e, KEY_ID));
	    			map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
	    			map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
	    			map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

	    			// adding HashList to ArrayList
	    			menuItems.add(map);
	    		}
	    		
	    		// Adding menuItems to ListView
	    		final ListAdapter adapter = new SimpleAdapter(this, menuItems,
	    				R.layout.list_item,
	    				new String[] { KEY_NAME , KEY_ID , KEY_DESC}, new int[] {
	    						R.id.name, R.id.cost ,R.id.desciption});
	    		final ListView lv = getListView();
	    		
	    		
	    		final EditText ed=(EditText)findViewById(R.id.EditText01);
	    		   			    		
	    		lv.setAdapter(adapter);
	    		
    		
	    		

	    		// selecting single ListView item
	    		
	    	/*	final EditText ed=(EditText)findViewById(R.id.EditText01);
	    		ed.addTextChangedListener(new TextWatcher() {
	    			int textlength=0;
	    			//String lv_arr[]={KEY_NAME};
	    			ArrayList<String> arr_sort= new ArrayList<String>();

	    			public void afterTextChanged(Editable s) {
	    			}

	    			public void beforeTextChanged(CharSequence s, int start, int count,
	    			int after) {
	    			}

	    			public void onTextChanged(CharSequence s, int start, int before,
	    			int count) {

	    			textlength=ed.getText().length();
	    			arr_sort.clear();
	    			for(int i=0;i<6000;i++)
	    			{
	    			if(textlength<=KEY_NAME.length())
	    			{
	    			if(ed.getText().toString().equalsIgnoreCase((String) KEY_NAME.subSequence(0, textlength)))
	    			{
	    			arr_sort.add(KEY_NAME);
	    			}
	    			}
	    			}

	    			lv.setAdapter(new ArrayAdapter<String>(Question.this,android.R.layout.simple_list_item_1 , arr_sort));

	    			}
	    			});
	    		*/
	    		
	    		
	    		/*	 ed.addTextChangedListener(new TextWatcher() {
	    			 
	    	            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
	    	                // When user changed the Text
	    	                Question.this.adapter.getFilter().filter(cs);
	    	            }
	    	 
	    	            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
	    	                    int arg3) {
	    	                // TODO Auto-generated method stub
	    	 
	    	            }
	    	 
	    	            public void afterTextChanged(Editable arg0) {
	    	                // TODO Auto-generated method stub
	    	            }
	    	        });
	    		
	    		 */
	    		
	    		
	    		
	    		lv.setOnItemClickListener(new OnItemClickListener() {

	    			public void onItemClick(AdapterView<?> parent, View view,
	    					int position, long id) {
	    				// getting values from selected ListItem
	    				String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
	    				//String gid = KEY_ID;
	    				String gid = ((TextView) view.findViewById(R.id.cost)).getText().toString();
	    				String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
	    				
	    				// Starting new intent
	    				Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
	    				in.putExtra(KEY_NAME, name);
	    				in.putExtra(KEY_ID, gid);
	    			//	in.putExtra(KEY_COST, cost);
	    				in.putExtra(KEY_DESC, description);
	    				startActivity(in);

	    			}
	    		});
	    		ed.addTextChangedListener(new TextWatcher() {
	    			 public void onTextChanged(CharSequence s, int start, int before,
	    			            int count) {
	    			        ((Filterable) adapter).getFilter().filter(s);
	    			    }
	 
	            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
	                    int arg3) {
	                // TODO Auto-generated method stub
	 
	            }
	 
	            public void afterTextChanged(Editable arg0) {
	                // TODO Auto-generated method stub
	            }}
	        );
	    		
	    		
	    		
	    		
	    		
	    		
	    		
	    	}
	        
	         
	      } catch (Exception e) {
	    	 // tv.setText("error: " + e);
	            e.printStackTrace();
	      }
	      } }
		
		
		
		
